package com.suxin.http.response.handler;

import com.alibaba.nacos.common.utils.JacksonUtils;
import com.suxin.http.model.Header;
import com.suxin.http.model.HttpRestResult;
import com.suxin.http.response.HttpClientResponse;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;

/**
 * @author Tang
 * @classname BeanResponseHandler
 * @description [  ]
 * @date 2023/3/2 22:19
 */
public class BeanResponseHandler<T> extends AbstractResponseHandler<T>{

    @Override
    protected HttpRestResult<T> convertHandler(HttpClientResponse response, Type type) throws IOException{
        final InputStream body = response.getBody();
        final Header headers = response.getHeaders();
        final int statusCode = response.getStatusCode();
        final T obj = JacksonUtils.toObj(body, type);
        return new HttpRestResult<T>(statusCode,null,obj,headers);
    }

}
